package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.ServiceInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

@Mapper
public interface ServiceMapper {

    @Select("SELECT * FROM t_service_marks WHERE pid = #{pid}")
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "service_name", property = "serviceName"),
            @Result(column = "service_desc", property = "serviceDesc"),
            @Result(column = "app_id", property = "appId"),
            @Result(column = "service_url", property = "serviceUrl"),
            @Result(column = "publish_client_id", property = "publishClientId"),
            @Result(column = "line_status", property = "lineStatus"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(property = "appInfo", column = "app_id",
                    one = @One(select = "com.spider.admin.store.primary.mapper.AppMapper.queryAppById", fetchType = FetchType.EAGER)),
            @Result(column = "service_doc_id", property = "serviceDocId")
    })
    ServiceInfo queryServiceById(String pid);

    @Insert("INSERT INTO t_service_marks(pid,service_name,service_desc,app_id,service_url,publish_client_id,service_doc_id,line_status,create_time,update_time,tenant_id)" +
            " VALUES(#{pid},#{serviceName},#{serviceDesc},#{appId},#{serviceUrl},#{publishClientId},#{serviceDocId},#{lineStatus},NOW(),NOW(),#{tenantId})")
    int saveService(ServiceInfo serviceInfo);

    @Update(value = {
            "<script>",
            " update t_service_marks ",
            " <set> ",
            " update_time=NOW() ",
            "<if test='serviceInfo.serviceName != null'>,service_name = #{serviceInfo.serviceName}</if>",
            "<if test='serviceInfo.serviceDesc != null'>,service_desc = #{serviceInfo.serviceDesc}</if>",
            "<if test='serviceInfo.appId != null'>,app_id = #{serviceInfo.appId}</if>",
            "<if test='serviceInfo.serviceUrl != null'>,service_url = #{serviceInfo.serviceUrl}</if>",
            "<if test='serviceInfo.serviceDocId != null'>,service_doc_id = #{serviceInfo.serviceDocId}</if>",
            "<if test='serviceInfo.lineStatus != null'>,line_status = #{serviceInfo.lineStatus}</if>",
            " </set> ",
            " where pid=#{pid} ",
            "</script>"
    })
    int updateService(ServiceInfo serviceInfo, String pid);

    @Select({"<script>",
            "Select count(0) from t_service_marks WHERE 1=1 ",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "<if test='curTenantId != null'> AND tenant_id = #{curTenantId}</if>",
            "<if test='serviceName != null'> AND service_name = #{serviceName}</if>",
            "<if test='appId != null'> AND app_id = #{appId}</if>",
            "</script>"})
    Long getServiceRecordNumByTenantId(String curTenantId,String tenantId, String serviceName, String appId);

    @Select({"<script>",
            "Select *  from t_service_marks WHERE 1=1 ",
            "<if test='curTenantId != null'> AND tenant_id = #{curTenantId}</if>",
            "<if test='tenantId != null'> AND tenant_id = #{tenantId}</if>",
            "<if test='serviceName != null'> AND service_name like '%#{serviceName}%'</if>",
            "<if test='appId != null'> AND app_id = #{appId}</if>",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "service_name", property = "serviceName"),
            @Result(column = "service_desc", property = "serviceDesc"),
            @Result(column = "app_id", property = "appId"),
            @Result(column = "service_url", property = "serviceUrl"),
            @Result(column = "publish_client_id", property = "publishClientId"),
            @Result(column = "line_status", property = "lineStatus"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(property = "appInfo", column = "app_id",
                    one = @One(select = "com.spider.admin.store.primary.mapper.AppMapper.queryAppById", fetchType = FetchType.EAGER)),
            @Result(column = "service_doc_id", property = "serviceDocId")
    })
    List<ServiceInfo> queryServiceListByTenantId(int startRow, int pageSize,String curTenantId, String tenantId, String serviceName, String appId);

    @Delete("delete from t_service_marks where pid=#{pid}")
    int remove(String pid);


    @Select({"Select tsm.pid,tsm.app_id,tai2.app_name, tsm.service_url,tsm.service_name,tsm.tenant_id,tai.tenant_name  from t_service_marks tsm,t_account_info tai,t_application_info tai2 WHERE tsm.tenant_id=tai.tenant_id AND tsm.app_id=tai2.pid"})
    List<Map<String, String>> queryServiceToTenant();
}
